package svc

import (
	"fmt"
	"github.com/shugen/models"
)

//Insert
func (s *Svc) InsertMenTgan(mtg *models.MenTgan) error {

	if _, err := s.Engine.Insert(mtg); err != nil {
		fmt.Println("新增失败:", mtg, err)
		return err
	}
	return nil
}

//Update
func (s *Svc) UpdateMenTgan(mtg *models.MenTgan) (*models.MenTgan, error) {

	mentgan := &models.MenTgan{}
	//title和type不需要更新
	affect, err := s.Engine.ID(string(mtg.MenTganId)).Omit("type").Omit("title").Update(mtg)
	if err != nil {
		return nil, err
	}
	if affect > 0 {
		s.Engine.ID(string(mtg.MenTganId)).Get(mentgan)
	}
	return mentgan, nil
}

//Select
func (s *Svc) SelectMenTgan(mtg *models.MenTgan) (*models.MenTgan, error) {

	mentgan := &models.MenTgan{}

	if _, err := s.Engine.Where("type = ?", mtg.Type).Get(mentgan); err != nil {
		return nil, err
	}
	return mentgan, nil
}

//Select List
func (s *Svc) ListMenTgan() ([]*models.MenTgan, error) {

	var list []*models.MenTgan

	if err := s.Engine.Find(&list); err != nil {
		return nil, err
	}
	return list, nil
}
